<template>
  <div class="setting">
    <el-card class="box-card">
      <!-- 进度条 -->
      <znxd-step :stepList="stepList" :currentStep="currentStep" @chooseStep="chooseStep"></znxd-step>
      <div style="height: 63vh;">
        <el-scrollbar>
          <!-- 数据源 -->
          <data-source-setting ref="formSource" mode="simpleTable" v-show="currentStep === 1" v-model="dataSource" :initData="initData"></data-source-setting>
          <!-- 目标库 -->
          <data-target-setting ref="fromTarget" mode="simpleTable" v-show="currentStep === 2" v-model="dataTarget" :initData="initData" :dataSource="dataSource"></data-target-setting>
          <!-- 配置任务调度 -->
          <!-- <scheduling-setting ref="fromScheduling" mode="simpleTable" v-show="currentStep === 3" v-model="dataScheduling" :initData="initData"></scheduling-setting> -->
        </el-scrollbar>
      </div>
      <el-divider></el-divider>
      <!-- 底部按钮 -->
      <div class="footer">
        <!-- <el-button type="primary" size="mini" @click="saveEvent()">暂存</el-button> -->
        <el-button type="primary" size="mini" v-if="currentStep > 1" @click="currentStep--">上一步</el-button>
        <el-button type="primary" size="mini" v-if="currentStep < 2" @click="nextEvent">下一步</el-button>
        <el-button size="mini" v-if="currentStep === 1" @click="$router.go(-1)">取消</el-button>
        <el-button type="success" size="mini" v-if="currentStep === 2" @click="saveEvent('submit')">提交</el-button>
      </div>
    </el-card>
  </div>
</template>
<script>
import dataSourceSetting from "./dataSource"
import dataTargetSetting from "./dataFileTarget"
// import schedulingSetting from "./scheduling"
import { query, config } from "@/api/task/dataFile"
// import { formatTime } from "@/utils/config.js"

export default {
  components: {
    dataSourceSetting,
    dataTargetSetting
    // schedulingSetting
  },
  data() {
    return {
      stepList: [
        {
          step: 1,
          label: "1、配置数据来源"
        },
        {
          step: 2,
          label: "2、配置目标数据源"
        }
        // {
        //   step: 3,
        //   label: "3、配置任务调度"
        // }
      ],
      currentStep: 1,
      taskId: null,
      initData: {},
      dataSource: {},
      dataTarget: {},
      // columnFilter: [],
      dataScheduling: {}
      // columnLink: {}
    }
  },
  methods: {
    chooseStep(item) {
      this.currentStep = item.step
    },
    nextEvent() {
      switch (this.currentStep) {
        case 1: {
          let well = this.$refs.formSource.validateForm()
          if (!well) return false
        }
          break
        default:
          break
      }
      this.currentStep++
    },
    getData() {
      let obj = {
        id: this.taskId
      }
      query(obj).then((res) => {
        // console.log(res, "配置列表")
        let data = res.data
        if (data.code === 200) {
          this.initData = {
            ...data.content
          }
        } else {
          this.$message({
            message: data.msg,
            type: "error"
          })
        }
      })
    },
    saveEvent(t) {
      // console.log(this.dataSource, this.dataTarget, this.dataScheduling)
      let saveEventApi = ""
      if (t === "submit") {
        let well = this.$refs.formSource.validateForm() && this.$refs.fromTarget.validateForm()
        console.log(well)
        if (!well) return false
        saveEventApi = config
      } else {
        saveEventApi = config
      }

      let jobDataFileEntity = {
        id: this.taskId
      }
      // 接口源
      jobDataFileEntity.fileType = this.dataSource.fileType
      jobDataFileEntity.firstColum = 1
      jobDataFileEntity.readerColumns = this.dataSource.multilistData
      // 目标库
      jobDataFileEntity.writerDatasourceType = this.dataTarget.databaseType
      jobDataFileEntity.writerDatasourceId = this.dataTarget.database
      jobDataFileEntity.isCreateTable = this.dataTarget.isCreateTable
      jobDataFileEntity.writerTables = this.dataTarget.tableName
      jobDataFileEntity.writerColumns = this.dataTarget.selection
      // 任务调度
      jobDataFileEntity.isStart = this.dataScheduling.isStart
      jobDataFileEntity.isExecution = this.dataScheduling.isExecution
      let formData = new FormData()
      formData.append("file", this.dataSource.file)
      formData.append("jobDataFileEntity", JSON.stringify(jobDataFileEntity))
      saveEventApi(formData)
        .then((res) => {
          // console.log(res)
          let data = res.data
          if (data.code === 200) {
            this.$message({
              message: data.msg || "操作成功",
              type: "success"
            })
            // tep跳转
            if (t === "submit") {
              this.$router.go(-1)
            }
          } else {
            // this.$message.error(data.msg);
            this.$message({
              message: data.msg,
              type: "error"
            })
          }
        })
        .catch(() => {
          // this.$message.error(error);
          this.$message({
            message: "操作失败",
            type: "error"
          })
        })
    }
  },
  mounted() {
    this.taskId = this.$route.query.id
    this.getData()
  }
};
</script>
<style lang="scss" scoped>
.setting {
  .footer {
    text-align: center;
  }
}
</style>
